//宝藏探测
#include <bits/stdc++.h>



using namespace std;

int bao[1000001];
priority_queue<int,vector<int>,greater<int> > Lmin_queue;
priority_queue<int> Lmax_queue;
priority_queue<int , vector<int>,greater<int> > Rmin_queue;
priority_queue<int> Rmax_queue;

int Lmax[1000001];
int Lmin[1000001];
int Rmax[1000001];
int Rmin[1000001];

int main() {
	int n,k;
	cin>>n>>k;
	for( int i=1; i<=n; i++)
		cin>> bao[i];

	Lmin_queue.push( bao[1]);
	Lmin[1]=bao[1];
	Lmax_queue.push( bao[1]);
	Lmax[1]=bao[1];

	for( int i=2; i<=n; i++) {
		Lmin_queue.push( bao[i]) ;
		Lmin[i]=Lmin_queue.top();
		Lmax_queue.push( bao[i]) ;
		Lmax[i]=Lmax_queue.top();
	}

	Rmin_queue.push( bao[n]);
	Rmin[n]=bao[n];
	Rmax_queue.push( bao[n]);
	Rmax[n]=bao[n];

	for( int i=n-1; i>=1; i--) {
		Rmin_queue.push( bao[i]) ;
		Rmin[i]=Rmin_queue.top();
		Rmax_queue.push( bao[i]) ;
		Rmax[i]=Rmax_queue.top();
	}

	/*
		for( int i=1; i<=n; i++) {
			cout << Lmin[i]<< " ";
		}
		cout <<endl;
		for( int i=1; i<=n; i++) {
			cout << Lmax[i]<< " ";
		}
		cout <<endl;
		for( int i=1; i<=n; i++) {
			cout << Rmin[i]<< " ";
		}
		cout <<endl;
		for( int i=1; i<=n; i++) {
			cout << Rmax[i]<< " ";
		}
		cout <<endl;*/

	int ans=INT_MAX;
	for( int i=1; i<=n-k-1; i++) {
		int j = i+ k+1;
		int t1 = max(  Lmax[i], Rmax[j]);
		int t2 = min(  Lmin[i], Rmin[j]);
		ans = min( ans, t1-t2  );
	}
	cout << ans;


	return 0;
}
